/** Helper to allow imports from this file. */
function include(filename) {
  var head = document.getElementsByTagName('head')[0];
  script = document.createElement('script');
  script.src = filename;
  script.type = 'text/javascript';
  head.appendChild(script)
}

include('debug.js');
include('math3d.js');
include('geplugin-helpers.js');
include('simulator.js');
include('driving-controls.js');
include('page-controls.js');

var DS_ge;
var DS_geHelpers;
var DS_map;

google.load("jquery", "1");
google.load("maps", "2.x");
google.load("earth", "1");

function DS_init() {
  $('#directions-form input').attr('disabled', 'disabled');
  $('#simulator-form input').attr('disabled', 'disabled');
  
  google.earth.createInstance(
    'earth',
    function(ge) {
      DS_ge = ge;
      DS_ge.getWindow().setVisibility(true);
      DS_ge.getLayerRoot().enableLayerById(DS_ge.LAYER_BUILDINGS, true);
      DS_ge.getLayerRoot().enableLayerById(DS_ge.LAYER_BORDERS, true);
      DS_geHelpers = new GEHelpers(DS_ge);
      
      DS_ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
      window.google.earth.addEventListener(DS_ge, 'frameend',
       function asdf() { DS_simulator.controlTick(); })
      
      DS_map = new GMap2($('#map-container').get(0));
      DS_map.setCenter(new GLatLng(37.4419, -122.1419), 13);
      DS_map.addControl(new GSmallMapControl());
      DS_map.enableContinuousZoom();

      $('#directions-form input').removeAttr('disabled');
    },
    function() {
      alert('Google earth loading failed!');
    });

  function onresize() {
    var clientHeight = document.documentElement.clientHeight;

    $('#route-details, #earth-container, #map-container').each(function() {
      $(this).css({
        height: (clientHeight - $(this).position().top - 100).toString() + 'px' });      
    });
  }
  
  $(window).resize(onresize);
  onresize();
}

google.setOnLoadCallback(DS_init);
